package com.shuqi.activity;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.shuqi.common.utils.Log4an;
import com.shuqi.common.utils.SharedPreferenceUtil;
import com.shuqi.controller.R;
import com.shuqi.model.BookContentSettings;
import com.shuqi.statistics.StatisticsEventID;
import com.shuqi.statistics.StatisticsUtils;

/**
 * 预览页
 * 
 * @author Jianda.Yang
 * 
 * @Date 2014-2-12 下午9:29:39
 */
public class BookContentMoreSetPreviewActivity extends ActivityBase implements OnClickListener {

    ToggleButton btnToggle = null;
    private TextView contentTv;
    private String strContent =
            "一个平庸的山村少年，几经转折最终踏入修仙门派，他如何以平凡的资质修得仙法，如何一步一步走向巅峰，跻身枭雄、宗师之列，谱下一曲逆天的仙道之路。";
    private boolean isFullScreen;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_bookcontentmoreset_preview);
        activityLogicForward();
        activityInitData();
    }

    @Override
    public void activityLogicForward() {
        btnToggle =
                (ToggleButton) findViewById(R.id.bookcontentmoresetpreview_notice_bar_on_or_off);
        contentTv = (TextView) findViewById(R.id.bookcontentmoresetpreview_content_tv);

        isFullScreen =
                SharedPreferenceUtil.getInstanceSharedPreferenceUtil().getBookContentFullScreen();
        btnToggle.setChecked(isFullScreen);
        if (isFullScreen) {
            contentTv.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
        } else {
            contentTv.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.page_top, 0, 0);
        }

        btnToggle.setOnClickListener(this);
        findViewById(R.id.bookcontentmoresetpreview_back_tv).setOnClickListener(this);
        findViewById(R.id.bookcontentmoresetpreview_linespace_sub_tv).setOnClickListener(this);
        findViewById(R.id.bookcontentmoresetpreview_linespace_add_tv).setOnClickListener(this);

    }

    @Override
    public void activityInitData() {
        // strContent = getIntent().getStringExtra("previewContent");
        contentTv.setText(strContent);
        add = getIntent().getIntExtra("previewLineSpace", BookContentSettings.MIN_LINESPACE);
        contentTv.setLineSpacing(add, 1);
    }

    private int add;

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.bookcontentmoresetpreview_back_tv:
                finish();
                break;
            case R.id.bookcontentmoresetpreview_linespace_sub_tv:
                if (add > BookContentSettings.MIN_LINESPACE) {
                    add -= 2;
                    contentTv.setLineSpacing(add, 1);
                    SharedPreferenceUtil.getInstanceSharedPreferenceUtil()
                            .saveBookContentLineSpace(add);
                    StatisticsUtils.onEvent(BookContentMoreSetPreviewActivity.this,
                            StatisticsEventID.BOOKCONTENT_DECREASE_LINESPACE);
                }
                break;
            case R.id.bookcontentmoresetpreview_linespace_add_tv:
                if (add < BookContentSettings.MAX_LINESPACE) {
                    add += 2;
                    contentTv.setLineSpacing(add, 1);
                    SharedPreferenceUtil.getInstanceSharedPreferenceUtil()
                            .saveBookContentLineSpace(add);
                    StatisticsUtils.onEvent(BookContentMoreSetPreviewActivity.this,
                            StatisticsEventID.BOOKCONTENT_ADD_LINESPACE);
                }
                break;
            case R.id.bookcontentmoresetpreview_notice_bar_on_or_off: {
                if (btnToggle.isChecked()) {
                    contentTv.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
                } else {
                    contentTv.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.page_top, 0, 0);
                }
                SharedPreferenceUtil.getInstanceSharedPreferenceUtil().saveBookContentFullScreen(
                        btnToggle.isChecked());
                StatisticsUtils.onEvent(BookContentMoreSetPreviewActivity.this,
                        StatisticsEventID.BOOKCONTENT_FULL_SCREEN);
            }
                break;
            default:
                break;
        }
    }
}
